package cz.uvn.orthopedy.client.ui.editor;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.CheckBox;

import cz.uvn.orthopedy.client.entity.Insurance;
import cz.uvn.orthopedy.client.entity.Patient;

public class PatientEditor extends Composite {
	private Patient patient;
	private Insurance[] insurancesCache;
	
	private TextBox nameTextBox;
	private TextBox surNameTextBox;
	private TextBox rcTextBox;
	private ListBox insuranceComboBox;
	private TextArea addressTextArea;
	private TextBox phoneTextBox;
	private TextBox emailTextBox;
	private CheckBox pnCheckBox;
	private CheckBox aboveStandardCheckBox;

	public PatientEditor(Patient patient, Insurance[] insurancesCache) {
		this.patient = patient;
		this.insurancesCache = insurancesCache;
		
		initUI();
	}
	
	private void initUI() {
		AbsolutePanel patientPanel = new AbsolutePanel();
		patientPanel.setSize("428px", "318px");

		Label lblName = new Label("Jméno:");
		lblName.setStyleName("boldLabel");
		lblName.setSize("100px", "18px");
		patientPanel.add(lblName, 10, 16);

		nameTextBox = new TextBox();
		nameTextBox.setSize("294px", "16px");
		patientPanel.add(nameTextBox, 116, 10);

		Label lblSurname = new Label("Příjmení:");
		lblSurname.setStyleName("boldLabel");
		lblSurname.setSize("100px", "18px");
		patientPanel.add(lblSurname, 10, 46);
		
		surNameTextBox = new TextBox();
		surNameTextBox.setSize("294px", "16px");
		patientPanel.add(surNameTextBox, 116, 40);
		
		Label lblRC = new Label("Rodné  Čislo:");
		lblRC.setStyleName("boldLabel");
		lblRC.setSize("100px", "18px");
		patientPanel.add(lblRC, 10, 76);
		
		rcTextBox = new TextBox();
		rcTextBox.setSize("294px", "16px");
		patientPanel.add(rcTextBox, 116, 70);
		
		Label lblInsurance = new Label("Pojistovna:");
		lblInsurance.setStyleName("boldLabel");
		lblInsurance.setSize("100px", "18px");
		patientPanel.add(lblInsurance, 10, 104);
		
		insuranceComboBox = new ListBox(false);
		for (int i = 0; i < insurancesCache.length; i++) {
			insuranceComboBox.addItem(insurancesCache[i].toString());
		}
		insuranceComboBox.setVisibleItemCount(1);
		insuranceComboBox.setSize("302px", "22px");
		patientPanel.add(insuranceComboBox, 116, 100);
		
		Label lblAddress = new Label("Bydliště:");
		lblAddress.setStyleName("boldLabel");
		lblAddress.setSize("100px", "18px");
		patientPanel.add(lblAddress, 10, 128);
		
		addressTextArea = new TextArea();
		addressTextArea.setSize("296px", "45px");
		patientPanel.add(addressTextArea, 116, 128);
		
		Label lblPhone = new Label("Telefon:");
		lblPhone.setStyleName("boldLabel");
		lblPhone.setSize("100px", "18px");
		patientPanel.add(lblPhone, 10, 195);
		
		phoneTextBox = new TextBox();
		phoneTextBox.setSize("294px", "16px");
		patientPanel.add(phoneTextBox, 116, 189);
		
		Label lblEmail = new Label("Email:");
		lblEmail.setStyleName("boldLabel");
		lblEmail.setSize("100px", "18px");
		patientPanel.add(lblEmail, 10, 225);
		
		emailTextBox = new TextBox();
		emailTextBox.setSize("294px", "16px");
		patientPanel.add(emailTextBox, 116, 219);
		
		pnCheckBox = new CheckBox("Pracovní Neschopnost");
		pnCheckBox.setStyleName("boldLabel");
		patientPanel.add(pnCheckBox, 10, 257);
		
		aboveStandardCheckBox = new CheckBox("Nadstandard");
		aboveStandardCheckBox.setStyleName("boldLabel");
		patientPanel.add(aboveStandardCheckBox, 10, 283);
		
		initWidget(patientPanel);
		
		if (patient != null) {
			nameTextBox.setText(patient.getName());
			surNameTextBox.setText(patient.getSurname());
			rcTextBox.setText(patient.getRodneCislo());
			for (int i = 0; i < insurancesCache.length; i++) {
				if (insurancesCache[i].getId().equals(patient.getInsuranceID())) {
					insuranceComboBox.setSelectedIndex(i);
					break;
				}
			}
			addressTextArea.setText(patient.getAddress());
			phoneTextBox.setText(patient.getPhoneNumber());
			emailTextBox.setText(patient.getEmail());
			pnCheckBox.setValue(patient.isWorkAbsence());;
			aboveStandardCheckBox.setValue(patient.isNadstandard());
		}
	}

	public Patient getPatinet() {
		Patient pat = patient == null ? new Patient() : patient;
		pat.setName(nameTextBox.getText());
		pat.setSurname(surNameTextBox.getText());
		pat.setRodneCislo(rcTextBox.getText());
		pat.setInsuranceID(insurancesCache[insuranceComboBox.getSelectedIndex()].getId());
		pat.setAddress(addressTextArea.getText());
		pat.setPhoneNumber(phoneTextBox.getText());
		pat.setEmail(emailTextBox.getText());
		pat.setWorkAbsence(pnCheckBox.getValue());
		pat.setNadstandard(aboveStandardCheckBox.getValue());
		return pat;
	}

	public boolean isValid() {
		if (nameTextBox.getText().length() < 1) {
			Window.alert("Name must not be empty");
			return false;
		}
		if (surNameTextBox.getText().length() < 1) {
			Window.alert("Surname must not be empty");
			return false;
		}
		if (rcTextBox.getText().length() != 10) {
			Window.alert("Rodne cislo must have 10 digits exactly");
			return false;
		}
		return true;
	}
}
